package com.springboot.blog.controller;

import com.springboot.blog.beans.*;
import com.springboot.blog.config.utils.R;
import com.springboot.blog.dao.*;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Controller
public class BlogSetController {

    @Resource
    private BlogSetDao blogSetDao;
    @Resource
    private BlogDao blogDao;
    @Resource
    private ClassifyDao classifyDao;
    @Resource
    private CommentDao commentDao;
    @Resource
    private UserDao userDao;
    @Resource
    private YouQingDao youQingDao;
    @Resource
    private BlogGgDao blogGgDao;
    @Resource
    private RedisTemplate<Object,Object> redisTemplate;

    @RequestMapping("admin/blogSet")
    public String blogSet(Model model){
        BlogSet blogsetinfo = new BlogSet();
        blogsetinfo.setBlogSetid(1);
        System.out.println(blogsetinfo);
        BlogSet infoset = blogSetDao.selectById(blogsetinfo);

        model.addAttribute("setblog",infoset);
        return "admin/website";
    }

    @RequestMapping("admin/updateSet")
    @ResponseBody
    public R updateSet(String title,String titles,String avatar,String keywords,String descript,String blogicp,String blogqq,String gonggao,String tongji,String banquan){
        BlogSet bloginfo = new BlogSet();
        bloginfo.setBlogSetid(1);
        bloginfo.setBlogSettitle(title);
        bloginfo.setBlogSettitles(titles);
        bloginfo.setBlogLogo(avatar);
        bloginfo.setBlogKeywords(keywords);
        bloginfo.setBlogDescription(descript);
        bloginfo.setBlogIcp(blogicp);
        bloginfo.setBlogQq(blogqq);
        bloginfo.setBlogGonggao(gonggao);
        bloginfo.setBlogTongji(tongji);
        bloginfo.setBlogBanquan(banquan);
        int update = blogSetDao.updateById(bloginfo);
        if (update>0){
            return R.ok();
        }
        return R.error();
    }

    @RequestMapping("admin/console")
    public String indexCount(Model model){
        //文章总数
        Integer blogindexcount = blogDao.selectCount(null);
        //统计当天添加文章条数
        int addCountBlog = blogDao.addCountBlog();
        //分类总数
        Integer classindexcount = classifyDao.selectCount(null);
        //评论总数
        Integer commentindexcount = commentDao.selectCount(null);
        //用户总数
        Integer userindexcount = userDao.selectCount(null);
        //首页访问量
        BlogSet blogSet = blogSetDao.selectSetView();
        //总访问量
        int allView = blogDao.selectAllView();
        model.addAttribute("blogcount",blogindexcount);
        model.addAttribute("addCountBlog",addCountBlog);
        model.addAttribute("classcount",classindexcount);
        model.addAttribute("commentcount",commentindexcount);
        model.addAttribute("usercount",userindexcount);
        model.addAttribute("setView",blogSet.getBlogView());
        model.addAttribute("allView",allView);
        return "admin/console";
    }

    @RequestMapping("/")
    public String login1(HttpServletRequest request,Model model) throws Exception {
        //设置 redisTemplate 对象 key 的序列化方式
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        String  userName = (String) request.getSession().getAttribute("userName");
        List<BlogSet> blogSets = (List<BlogSet>) redisTemplate.opsForValue().get("blogSets");
        if (null== blogSets ){
            blogSets = blogSetDao.selectList(null);
            redisTemplate.opsForValue().set("blogSets",blogSets,30, TimeUnit.SECONDS);
        }
        model.addAttribute("index",blogSets);

        List<User> allUser = (List<User>) redisTemplate.opsForValue().get("allUser");
        if (null == allUser){
            allUser = userDao.findAllUser();
            redisTemplate.opsForValue().set("allUser",allUser,30, TimeUnit.SECONDS);
        }
        model.addAttribute("allUser",allUser);

        List<Blog> blogList = (List<Blog>) redisTemplate.opsForValue().get("blogList");
        if (null==blogList){
            blogList = blogDao.selectAllBlog();
            redisTemplate.opsForValue().set("blogList",blogList,30, TimeUnit.SECONDS);
        }
        model.addAttribute("blogList",blogList);
        blogSetDao.updateSetView();

        List<YouQing> youQings = (List<YouQing>) redisTemplate.opsForValue().get("youQings");
        if (null == youQings){
            youQings = youQingDao.selectList(null);
            redisTemplate.opsForValue().set("youQings",youQings,30, TimeUnit.SECONDS);
        }
        model.addAttribute("youQings",youQings);

        List<BlogClassify> blogClassifies = (List<BlogClassify>) redisTemplate.opsForValue().get("blogClassifies");
        if (null == blogClassifies){
            blogClassifies = classifyDao.selectList(null);
            redisTemplate.opsForValue().set("blogClassifies",blogClassifies,30, TimeUnit.SECONDS);
        }
        model.addAttribute("blogClassifies",blogClassifies);

        List<BlogGonggao> gongao = (List<BlogGonggao>) redisTemplate.opsForValue().get("gongao");
        if (null == gongao){
            gongao = blogGgDao.findGongao();
            redisTemplate.opsForValue().set("gongao",gongao,30, TimeUnit.SECONDS);
        }
        model.addAttribute("gongao",gongao);


        User user = (User) redisTemplate.opsForValue().get("user");
        if (null == user){
            user = userDao.queryByUsername(userName);
            redisTemplate.opsForValue().set("user",user,30, TimeUnit.SECONDS);
        }
        model.addAttribute("user",user);
        return "index";
    }
}
